#include "errorExtension.h"

/****************************************************************************************
*
*	Mathematica Error Handling!
*
****************************************************************************************/

void MLReturnError(const char *fname, const char *msgtag){
    MLPutFunction(stdlink,"CompoundExpression",2);
        MLPutFunction(stdlink,"Message",1);
                MLPutFunction(stdlink,"MessageName",2);
                    MLPutSymbol(stdlink,fname);
                    MLPutString(stdlink,msgtag);
        MLPutSymbol(stdlink,"$Failed");
}

void MLReturnError(const char *fname, const char *msgtag, const char *str1){
    MLPutFunction(stdlink,"CompoundExpression",2);
        MLPutFunction(stdlink,"Message",3);
                MLPutFunction(stdlink,"MessageName",2);
                    MLPutSymbol(stdlink,fname);
                    MLPutString(stdlink,msgtag);
                MLPutString(stdlink,str1);
        MLPutSymbol(stdlink,"$Failed");
}

void MLReturnError(const char *fname, const char *msgtag, const char *str1, const char *str2){
    MLPutFunction(stdlink,"CompoundExpression",2);
        MLPutFunction(stdlink,"Message",3);
                MLPutFunction(stdlink,"MessageName",2);
                    MLPutSymbol(stdlink,fname);
                    MLPutString(stdlink,msgtag);
                MLPutString(stdlink,str1);
                MLPutString(stdlink,str2);
        MLPutSymbol(stdlink,"$Failed");
}

#include <string>

bool MLAttempt(int func){
    if (!func) {
        char err_msg[150];
        sprintf(err_msg, "Message[GrapheneToolsLink::error,\"%.76s\"]", MLErrorMessage(stdlink));
        MLClearError(stdlink); MLNewPacket(stdlink); MLEvaluate(stdlink, err_msg);
        MLNextPacket(stdlink); MLNewPacket(stdlink); MLPutSymbol(stdlink, "$Failed");
        return false;
    }
    return true;
}